//其他装置
var DeviceConnectChart = {
    DeviceSpace:120,//设备间隔
    parseFrom: function(unit_id, deviceConnectData){
        /*
        {
            "count": 68,
            "list": [
                {
                    "PATH_ID": 55,
                    "PORT_DIRECTION": "Tx",
                    "PORT_ID": 40,
                    "PORT_INDEX": 0,
                    "PORT_NAME": "2X-C",
                    "PORT_PLUG": "LC",
                    "TYPE": "GO",
                    "UNIT_CLASS": "IED",
                    "UNIT_DESC": "#1主变保护装置A",
                    "UNIT_ID": 4
                },
                ...
            ]
        }
         */
        Device.width = 270; //装置宽度
        Port.width = 130; //装置内部端口宽度

        var chart = {};
        chart.x=0;
        chart.y=0;
        chart.oriData = deviceConnectData;
        chart.unit_id = unit_id;//主装置号
        chart.allDrawedPorts = [];//保存要画的端口
        chart.device = null;//保存自身
        //开始解析路径，查找要画出的装置端口
        var pathDic = [];
        chart.pathDic = pathDic;
        for(var i=0;i < chart.oriData.list.length;i++){
            var data = chart.oriData.list[i];
            if(!pathDic.hasOwnProperty(data.PATH_ID)){
                pathDic[data.PATH_ID] = [];
            }
            pathDic[data.PATH_ID].push(data);
        }
        pathDic.sort(DeviceConnectChart.PathArrAscSort);
        //路径排序，并记录下要画的端口及路径
        var dataArr = [];
        for(var k in pathDic){
            pathDic[k].sort(DeviceConnectChart.PathAscSort);
            if(pathDic[k][0].UNIT_ID != unit_id){
                continue;//路径开头不是本设备，则跳过
            }
            //优化每条路径，剔除内部转发
            var path = [];
            for(var i=0;i<pathDic[k].length;i++){
                var item = pathDic[k][i];
                if(path.length > 0 && i < pathDic[k].length - 1){
                    var lastPathItem = path[path.length - 1];
                    var nextItem = pathDic[k][i + 1];
                    if(item.UNIT_ID == lastPathItem.UNIT_ID && item.UNIT_ID == nextItem.UNIT_ID){
                        continue;
                    }
                }
                path.push(item);
            }

            dataArr.push(path);
            if(chart.device == null){
                chart.device = Device.createNew(path[0].UNIT_ID, path[0].UNIT_DESC);
                chart.device.data = path[0];
                chart.device.leftPorts = [];
                chart.device.rightPorts = [];
                chart.device.devices = [];
            }
        }
        //初始化右侧显示的端口个数
        DeviceConnectChart.initRightPort(chart.device,dataArr, 0);
        //端口按照名称排序
        chart.device.rightPorts.sort(DeviceConnectChart.PortAscSort);
        //增加一些方法
        chart.getTotalWidth = function(){
            return DeviceConnectChart.getDeviceWidth(chart.device);
        };
        chart.getTotalHeight = function(){
            return DeviceConnectChart.getDeviceHeight(chart.device);
        }
        chart.draw = function(canvas){
            DeviceConnectChart.draw(this, canvas);
        };
        chart.getPortByPoint = function(x ,y){
            for(var i=0;i<this.allDrawedPorts.length;i++){
                //1.点击的是不是此交换机接口？
                var p = this.allDrawedPorts[i];
                //上半部分是of_id
                if(p.x < x && p.x + Port.width > x && p.y < y && p.y + Port.height / 2 > y){
                    return {
                        UNIT_ID : p.data.UNIT_ID,
                        PORT_ID : p.data.PORT_ID,
                        TYPE : 'PORT'
                    }
                }
                //下半部分是if_id
                if(p.x < x && p.x + Port.width > x && p.y + Port.height / 2 < y && p.y + Port.height > y){
                    return {
                        UNIT_ID : p.data.UNIT_ID,
                        PORT_ID : p.data.PORT_ID,
                        TYPE : 'PORT'
                    }
                }
            }
            return null;
        }
        return chart;
    },
    PathAscSort : function (a,b)
    {
        return a.PORT_INDEX - b.PORT_INDEX;
    },
    PortAscSort : function (a,b)
    {
        if( a.name > b.name){
            return 1;
        }
        if( a.name < b.name){
            return -1;
        }
        return 0;
    },
    PathArrAscSort:function(a,b){
        if( a[0].PORT_NAME > b[0].PORT_NAME){
            return 1;
        }
        if( a[0].PORT_NAME < b[0].PORT_NAME){
            return -1;
        }
        return 0;
    },
    draw : function(chart, canvas){
        chart.allDrawedPorts = [];
        //先画主装置
        var obj = chart.device;
        obj.x = chart.x;
        obj.y = chart.y;
        Device.drawTitle(obj, canvas);
        //绘制主题色
        canvas.fillStyle = Device.bodyBackground;
        canvas.fillRect(obj.x,obj.y + Device.headerHeight,Device.width,DeviceConnectChart.getDeviceHeight(obj) - Device.headerHeight);
        DeviceConnectChart.drawLPortAndRDevice(obj, chart, canvas);
    },
    drawLPortAndRDevice : function(obj, chart, canvas){
        var leftDeviceRightPortIndex = 0;
        var leftDeviceRightX = obj.x + obj.width - Port.width;
        var leftDeviceRightY = obj.y +  Device.headerHeight;
        if(obj.leftPorts.length == 0){
            var leftDeviceHasDrawBodyY = obj.y + Device.headerHeight;
        }else{
            var leftDeviceHasDrawBodyY = obj.y + Device.headerHeight + obj.leftPorts.length * Port.height + (obj.leftPorts.length - 1) * 10;
        }
        //画与本装置直接相连的那一列装置，及其左侧端口
        for(var i=0; i<obj.devices.length; i++){
            var d = obj.devices[i];
            d.x = leftDeviceRightX + Port.width + DeviceConnectChart.DeviceSpace;
            d.y = leftDeviceRightY - Device.headerHeight;
            //画设备
            Device.drawTitle(d, canvas);
            //画主体部分,先按左侧端口数铺底
            canvas.fillStyle = Device.bodyBackground;
            canvas.fillRect(d.x,d.y + Device.headerHeight,Device.width,d.leftPorts.length * Port.height + (d.leftPorts.length - 1) * 10);
            //画端口
            for(var j=0; j<d.leftPorts.length; j++){
                //先画左侧设备右端口
                var pLeft = obj.rightPorts[leftDeviceRightPortIndex];
                pLeft.x = leftDeviceRightX;
                pLeft.y = d.y + Device.headerHeight + j * (10  + Port.height);
                pLeft.direct = 'right';
                //先看看是不是要画底图
                if(pLeft.y + Port.height > leftDeviceHasDrawBodyY){
                    canvas.fillStyle = Device.bodyBackground;
                    var newDrawBodyY = pLeft.y + Port.height;
                    canvas.fillRect(obj.x,leftDeviceHasDrawBodyY,Device.width,newDrawBodyY - leftDeviceHasDrawBodyY);
                    leftDeviceHasDrawBodyY = newDrawBodyY;
                }
                Port.draw(pLeft,canvas);
                chart.allDrawedPorts.push(pLeft);
                leftDeviceRightPortIndex++;

                var p = d.leftPorts[j];
                p.x = d.x;
                p.y = d.y  + Device.headerHeight + j * (10 + Port.height);
                p.direct = 'left';
                Port.draw(p,canvas);
                chart.allDrawedPorts.push(p);
                //画连接线
                var path = Path.createNew(1, p, pLeft, p.data.TYPE);
                Path.draw(path, canvas);
                path = Path.createNew(1, pLeft,p , p.data.TYPE);
                Path.draw(path, canvas);
            }
            leftDeviceRightY += 10 + DeviceConnectChart.getDeviceHeight(d);
            //递归的画此设备
            DeviceConnectChart.drawLPortAndRDevice(d, chart, canvas);
        }
    },
    getDeviceWidth : function(device){
        if(device.devices.length == 0){
            return Device.width;
        }
        var maxWidth = 0;
        for(var k in device.devices){
            var w = DeviceConnectChart.getDeviceWidth(device.devices[k]);
            if(maxWidth < w){
                maxWidth = w;
            }
        }
        return Device.width + DeviceConnectChart.DeviceSpace + maxWidth;
    },
    getDeviceHeight : function(device){
        var leftPortsHeight = device.leftPorts.length * Port.height + (device.leftPorts.length - 1) * (10);
        var rightPortsHeight = 0;
        for(var k in device.devices){
            var dHeight = DeviceConnectChart.getDeviceHeight(device.devices[k]);
            if(rightPortsHeight > 0){
                rightPortsHeight;
            }
            rightPortsHeight += dHeight;
        }
        return Math.max(leftPortsHeight, rightPortsHeight) + Device.headerHeight;
    },
    createPath : function(p1,p2,port1,port2){
        /*p1.data:{
                "PATH_ID": 55,
                "PORT_DIRECTION": "Tx",
                "PORT_ID": 40,
                "PORT_INDEX": 0,
                "PORT_NAME": "2X-C",
                "PORT_PLUG": "LC",
                "TYPE": "GO",
                "UNIT_CLASS": "IED",
                "UNIT_DESC": "#1主变保护装置A",
                "UNIT_ID": 4
            }
         */
        if(p1.PORT_DIRECTION == "Rx"){
            var p = Path.createNew(1, p2, port1, p1.TYPE);
            return p;
        } else if(p1.PORT_DIRECTION == "Tx"){
            var p = Path.createNew(1, port1, port2, p1.TYPE);
            return p;
        } else if(p2.PORT_DIRECTION == "Rx"){
            var p = Path.createNew(1, port1, port2, p1.TYPE);
            return p;
        } else{
            var p = Path.createNew(1, port2, port1, p1.TYPE);
            return p;
        }
    },
    initRightPort : function(theDevice, dataArr, dataStartIndex){
        //dataArr是双重数组，
        //先获取第一个与第二个设备对
        var dataTuble = [];
        for(var i = 0; i < dataArr.length; i++){
            if(dataArr[i].length <= dataStartIndex){
                return;
            }
            if(dataArr[i][dataStartIndex].UNIT_ID != theDevice.id){
                break;//路径开头不是本设备，则退出
            }
            dataTuble.push({
                data1:dataArr[i][dataStartIndex],
                data2:dataArr[i][dataStartIndex + 1],
                alldata:dataArr[i]
            });        
        }
        if(dataTuble.length < 1 || dataTuble[0].data2 == null || dataTuble[0].data2 == undefined){
            return;
        }
        //只有两个设备端口相同时，才能合并
        var device = Device.createNew(dataTuble[0].data2.UNIT_ID,dataTuble[0].data2.UNIT_DESC);
        device.data = dataTuble[0].data2;
        device.leftPorts = [];
        device.rightPorts = [];
        device.devices = [];
        device.dataArr = [dataTuble[0].alldata];
        theDevice.devices.push(device);

        var port = Port.createNew(dataTuble[0].data2.PORT_NAME);
        port.data = dataTuble[0].data2;
        port.datas = [dataTuble[0].data2];
        device.leftPorts.push(port);

        var prePort = Port.createNew(dataTuble[0].data1.PORT_NAME);
        prePort.data = dataTuble[0].data1;
        prePort.datas = [dataTuble[0].data1];
        prePort.tartgetDevice = device;
        theDevice.rightPorts.push(prePort);
        for(var i = 1; i < dataTuble.length; i++){
            if(dataTuble[i].data2.UNIT_ID == prePort.tartgetDevice.data.UNIT_ID &&
                dataTuble[i].data1.PORT_NAME == prePort.data.PORT_NAME &&
                dataTuble[i].data2.PORT_NAME == prePort.tartgetDevice.data.PORT_NAME){
                    //合并,跳过
                    prePort.tartgetDevice.dataArr.push(dataTuble[i].alldata);
            }else{
                if(dataTuble[i].data2.UNIT_ID != prePort.tartgetDevice.data.UNIT_ID){
                    //换了设备，要重新创建设备和端口了
                    var device = Device.createNew(dataTuble[i].data2.UNIT_ID,dataTuble[i].data2.UNIT_DESC);
                    device.data = dataTuble[i].data2;
                    device.leftPorts = [];
                    device.rightPorts = [];
                    device.devices = [];
                    device.dataArr = [dataTuble[i].alldata];
                    theDevice.devices.push(device);
            
                    var port = Port.createNew(dataTuble[i].data2.PORT_NAME);
                    port.data = dataTuble[i].data2;
                    port.datas = [dataTuble[i].data2];
                    device.leftPorts.push(port);
            
                    prePort = Port.createNew(dataTuble[i].data1.PORT_NAME);
                    prePort.data = dataTuble[i].data1;
                    prePort.datas = [dataTuble[i].data1];
                    prePort.tartgetDevice = device;
                    theDevice.rightPorts.push(prePort);
                }else{
                    //换了端口，只需要重新加端口就行了
                    prePort.tartgetDevice.dataArr.push(dataTuble[i].alldata);

                    var port = Port.createNew(dataTuble[i].data2.PORT_NAME);
                    port.data = dataTuble[i].data2;
                    port.datas = [dataTuble[i].data2];
                    prePort.tartgetDevice.leftPorts.push(port);
                    
                    var p = Port.createNew(dataTuble[i].data1.PORT_NAME);
                    p.data = dataTuble[i].data1;
                    p.datas = [dataTuble[i].data1];
                    p.tartgetDevice = prePort.tartgetDevice;
                    theDevice.rightPorts.push(p);
                    prePort= p;
                }
            }
        }
        //递归的对后面的设备进行初始化
        for(var k in theDevice.devices){
            DeviceConnectChart.initRightPort(theDevice.devices[k], theDevice.devices[k].dataArr,dataStartIndex + 2);
        }
    }
};